---------------------------------------------
-- The price coefficient for hiring new units
---------------------------------------------
-- Change this to the desired value. The resulting value will be rounded down to the nearest integer.
local ARMY_SHOP_HIRE_COEFF = 2.0

local unit_choice = 0
--****************************
--* Army calculation
--****************************

function army_shop_new_army(pos)

    local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
--	local army_slot = get_army_slot()
	local unit_type = army_atom[pos]
	-- Get the price of a single unit
	local price_per_unit = get_unit_price (unit_type)
	-- Basic sanity check to see that we got a "valid" unit
	if price_per_unit == 0 then
		return 0, 0, 0
	end
	-- Check how much gold the hero has - and how many units can he buy from that
	local has_this_much_gold = Logic.hero_lu_item("money", "count")
	local can_afford_this_many_units = math.floor(has_this_much_gold / (price_per_unit * ARMY_SHOP_HIRE_COEFF))

	-- Get the sum of all units of the same type
	local total_unit_count = get_unit_total_count(unit_type)

	-- Check how many leadership points the units take up - and how many recruits can be hired
	local leadership_per_unit = Logic.cp_leadship (unit_type)
	local army_leadership_value = leadership_per_unit * total_unit_count
	local hero_leadership_limit = Logic.hero_lu_item("leadership","count")
	local hero_leadership_available = hero_leadership_limit - army_leadership_value
	local units_allowed_to_buy = 0

	-- See if hero can buy at least one additional unit
	if hero_leadership_available >= leadership_per_unit then
		units_allowed_to_buy = math.floor (hero_leadership_available/leadership_per_unit)
		-- Handle the case when the buyer has less gold than the number of units he could buy otherwise
		if can_afford_this_many_units < units_allowed_to_buy then
			units_allowed_to_buy = can_afford_this_many_units
		end
	end

	-- Calculate the price of the units
	local value_of_troops_to_buy = math.floor(ARMY_SHOP_HIRE_COEFF*price_per_unit*units_allowed_to_buy)
	-- Return the total number of units the hero already has, the number of recruits to hire and the price to pay for the recruits.
	return total_unit_count, units_allowed_to_buy, value_of_troops_to_buy
end

function get_army_shop_hire_coeef()
	return tostring(ARMY_SHOP_HIRE_COEFF)
end

function get_army_max_count()
	local unit_count, unit_buy, value = army_shop_new_army(unit_choice)
	return tostring(unit_buy)
end

function get_army_shop_info()
	local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
	local shop_info = ""
	for i in pairs(army_atom) do
		if army_count[i]>0 then
			local price_per_unit = get_unit_price (army_atom[i])*ARMY_SHOP_HIRE_COEFF
	        local leadership_per_unit = Logic.cp_leadship (army_atom[i])
			local unit = "<label=cpn_"..army_atom[i]..">".."    ".."<image=quests_icon_money.png>"..tostring(price_per_unit).."    ".."<label=army_leadership>"..tostring(leadership_per_unit).."<br>"
			shop_info = shop_info..unit
		end
	end
	return shop_info
end

function get_army_per_price()
    local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
	local price_per_unit = get_unit_price (army_atom[unit_choice])*ARMY_SHOP_HIRE_COEFF
	return tostring(price_per_unit)
end

function get_army_max_money()
	local unit_count, unit_buy, value = army_shop_new_army(unit_choice)
	return tostring(value)
end

function get_army_shop_exit1()
    return "<label=dlg_exit>"

end

function get_army_shop_exit2()
    return "<label=dlg_exit>"
end

function get_army_type6()
	local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
	return "<label=cpn_"..army_atom[unit_choice]..">"
end

function get_army_type(pos)
    local army_atom,army_count = get_army_table(Logic.hero_lu_army(),5)
	return "<label=cpn_"..army_atom[pos]..">"

end

function get_army_type1()
   return get_army_type(1)
end

function get_army_type2()
   return get_army_type(2)
end

function get_army_type3()
   return get_army_type(3)
end

function get_army_type4()
   return get_army_type(4)
end

function get_army_type5()
   return get_army_type(5)
end

----------------------------------------
-- Purchase army into the indicated slot
----------------------------------------
function army_shop_purchase(n)
	local success = false -- Flag to signal successful transaction
	local units_buy = 0
	local units_cost = 0
	local hero_army_shop_new
	if unit_choice ~= 0 then
		local hero_gold_amount = get_gold_count()
		-- Backup the entire army (all 7 slots - they will be needed later on)
		local complete_army = Logic.hero_lu_army()
		local unit_type_in_slot1, unit_count_in_slot1 = Logic.army_get(complete_army, 1)
		local unit_type_in_slot2, unit_count_in_slot2 = Logic.army_get(complete_army, 2)
		local unit_type_in_slot3, unit_count_in_slot3 = Logic.army_get(complete_army, 3)
		local unit_type_in_slot4, unit_count_in_slot4 = Logic.army_get(complete_army, 4)
		local unit_type_in_slot5, unit_count_in_slot5 = Logic.army_get(complete_army, 5)
		local unit_type_in_slot6, unit_count_in_slot6 = Logic.army_get(complete_army, 6)
		local unit_type_in_slot7, unit_count_in_slot7 = Logic.army_get(complete_army, 7)
		-- Take an extra copy for the slot we are working with
		local unit_type_in_slot, unit_count_in_slot = Logic.army_get(complete_army, unit_choice)
		local unit_count_in_slot_new = 0
		if unit_count_in_slot > 0 then
			local unit_count_in_slot_all, unit_count_in_slot_to_buy, unit_value_in_slot = army_shop_new_army(unit_choice)
			if n==1 then
				units_buy =1
	        elseif n==2 then
		        units_buy = 10
	        elseif n==3 then
		        units_buy = 100
	        elseif n==4 then
		        units_buy = unit_count_in_slot_to_buy
	        end
			if unit_count_in_slot_to_buy > 0 and unit_count_in_slot_to_buy >= units_buy then
				units_cost = get_unit_price (unit_type_in_slot)*units_buy*ARMY_SHOP_HIRE_COEFF
				hero_money_new = hero_gold_amount - units_cost
				if hero_money_new >= 0 then
					-- Replace the entire army with the backups, but update the slot where the units have been purchased.
					if unit_choice == 1 then
						unit_count_in_slot_new=unit_count_in_slot1+tonumber(units_buy)
						hero_army_shop_new=unit_type_in_slot1 .."|".. unit_count_in_slot_new.."|"..unit_type_in_slot2.."|"..unit_count_in_slot2.."|"..unit_type_in_slot3.."|"..unit_count_in_slot3.."|"..unit_type_in_slot4.."|"..unit_count_in_slot4.."|"..unit_type_in_slot5.."|"..unit_count_in_slot5.."|"..unit_type_in_slot6.."|"..unit_count_in_slot6.."|"..unit_type_in_slot7.."|"..unit_count_in_slot7
					elseif unit_choice == 2 then
						unit_count_in_slot_new=unit_count_in_slot2+tonumber(units_buy)
						hero_army_shop_new=unit_type_in_slot1 .."|".. unit_count_in_slot1.."|"..unit_type_in_slot2.."|"..unit_count_in_slot_new.."|"..unit_type_in_slot3.."|"..unit_count_in_slot3.."|"..unit_type_in_slot4.."|"..unit_count_in_slot4.."|"..unit_type_in_slot5.."|"..unit_count_in_slot5.."|"..unit_type_in_slot6.."|"..unit_count_in_slot6.."|"..unit_type_in_slot7.."|"..unit_count_in_slot7
					elseif unit_choice == 3 then
						unit_count_in_slot_new=unit_count_in_slot3+tonumber(units_buy)
						hero_army_shop_new=unit_type_in_slot1 .."|".. unit_count_in_slot1.."|"..unit_type_in_slot2.."|"..unit_count_in_slot2.."|"..unit_type_in_slot3.."|"..unit_count_in_slot_new.."|"..unit_type_in_slot4.."|"..unit_count_in_slot4.."|"..unit_type_in_slot5.."|"..unit_count_in_slot5.."|"..unit_type_in_slot6.."|"..unit_count_in_slot6.."|"..unit_type_in_slot7.."|"..unit_count_in_slot7
					elseif unit_choice == 4 then
						unit_count_in_slot_new=unit_count_in_slot4+tonumber(units_buy)
						hero_army_shop_new=unit_type_in_slot1 .."|".. unit_count_in_slot1.."|"..unit_type_in_slot2.."|"..unit_count_in_slot2.."|"..unit_type_in_slot3.."|"..unit_count_in_slot3.."|"..unit_type_in_slot4.."|"..unit_count_in_slot_new.."|"..unit_type_in_slot5.."|"..unit_count_in_slot5.."|"..unit_type_in_slot6.."|"..unit_count_in_slot6.."|"..unit_type_in_slot7.."|"..unit_count_in_slot7
					elseif unit_choice == 5 then
						unit_count_in_slot_new=unit_count_in_slot5+tonumber(units_buy)
						hero_army_shop_new=unit_type_in_slot1 .."|".. unit_count_in_slot1.."|"..unit_type_in_slot2.."|"..unit_count_in_slot2.."|"..unit_type_in_slot3.."|"..unit_count_in_slot3.."|"..unit_type_in_slot4.."|"..unit_count_in_slot4.."|"..unit_type_in_slot5.."|"..unit_count_in_slot_new.."|"..unit_type_in_slot6.."|"..unit_count_in_slot6.."|"..unit_type_in_slot7.."|"..unit_count_in_slot7
					end
					success = true
				else
					Game.InvokeMsgBox("warning","<label=money_not_enough>")
				end
			end
		end
	end
	if success == true then
		-- Give the new army to the hero
		Logic.hero_lu_army(hero_army_shop_new)
	    -- Take the money from the hero
	    Logic.hero_lu_item("money", "count", hero_money_new)
	end
end

function army_shop_dlg_unit_condition(n)
	local success=false
	Logic.hero_lu_var("army_shop_dlg_unit",0)
	local army_slot = get_army_slot()
	if army_slot[n] then
		success = true
	end
	if success then
	   Logic.hero_lu_var("army_shop_dlg_unit",n)
	end

	return success
end

function army_shop_dlg_unit_condition1(param)
   return army_shop_dlg_unit_condition(1)
end

function army_shop_dlg_unit_condition2(param)
   return army_shop_dlg_unit_condition(2)
end

function army_shop_dlg_unit_condition3(param)
   return army_shop_dlg_unit_condition(3)
end

function army_shop_dlg_unit_condition4(param)
   return army_shop_dlg_unit_condition(4)
end

function army_shop_dlg_unit_condition5(param)
   return army_shop_dlg_unit_condition(5)
end

function army_shop_dlg_unit_choice(x)
	unit_choice = tonumber(x)
	return true
end

function army_shop_dlg_unit_choice1(param)
   return army_shop_dlg_unit_choice(1)
end

function army_shop_dlg_unit_choice2(param)
   return army_shop_dlg_unit_choice(2)
end

function army_shop_dlg_unit_choice3(param)
   return army_shop_dlg_unit_choice(3)
end

function army_shop_dlg_unit_choice4(param)
   return army_shop_dlg_unit_choice(4)
end

function army_shop_dlg_unit_choice5(param)
   return army_shop_dlg_unit_choice(5)
end

----------------------------------------------------------------
-- Determine whether we show a choice to add creatures to slot n
----------------------------------------------------------------
function army_shop_dlg_buy(n)
	army_shop_purchase(n)
	return true
end

----------------------------------------------------------------
-- Determine whether we show a choice to add creatures to slot 1
----------------------------------------------------------------
function army_shop_dlg_buy1(param) --ftag:if
	return army_shop_dlg_buy(1)
end

----------------------------------------------------------------
-- Determine whether we show a choice to add creatures to slot 2
----------------------------------------------------------------
function army_shop_dlg_buy2(param) --ftag:if
	return army_shop_dlg_buy(2)
end

----------------------------------------------------------------
-- Determine whether we show a choice to add creatures to slot 3
----------------------------------------------------------------
function army_shop_dlg_buy3(param) --ftag:if
	return army_shop_dlg_buy(3)
end

----------------------------------------------------------------
-- Determine whether we show a choice to add creatures to slot 4
----------------------------------------------------------------
function army_shop_dlg_buy4(param) --ftag:if
	return army_shop_dlg_buy(4)
end

